// import {throttle} from "@/utils/index"
import { actionByType } from '@/api/manage'
/* 
列表页常用增删改查的公共方法
flag 表单校验的结果 true false
form 表单数据
*/
export function actionByTypeNew(methods, url, params,that,from,name){

    //from 来自于哪个按钮 如编辑update删除delete新增add等
    //   //name 需要操作的名称 
    //处理管理员接口数据name
    console.log("进入actionByTypeNew",from)
    let newParams
    if(url==="/account/"){
      if(from==="add"){
        newParams=JSON.parse(JSON.stringify(params))
        newParams.name=newParams.accountName
      }else if(from==="update"){
        console.log("进入update",params)
        newParams={
          id:params.accountId,
          name:params.accountName,
        }
      }
      // newParams.name=newParams.accountName
      // newParams.deleted("accountName")
    }
    else if(url==="/permission/account-role/bind"){
      newParams={
        accountId:params.accountId,
        roleIds:params.selectRoles,
      }
    }else{
      newParams=params
    }
    return actionByType(methods, url, newParams,that,from,name).then(res => {
        if(res.data && res.data.code == 200){
            let message="成功"
            console.log({from})
            name=name||""
            switch (from){
              case "add": 
                message=`新增${name}成功!`;
                break
              case "update": 
                message=`编辑${name}成功!`;
                break
              case "delete": 
                 message=`删除${name}成功!`;
                 break
              case "deleteAll":
                 message=`批量删除${name}成功!`;
                 break
              default:
                 message=`新增${name}成功!`;
            }
            console.log({message})
            that.$message({
                message ,
                type : 'success'
            });
            if(from==="delete"){
              // console.log("delete-that.pagination",that.$refs.formPage.pagination)
              console.log("that.pageParam.initData.pagination",that.pageParam.initData.pagination)
              if (
                that.pageParam.initData.pagination.total > that.pageParam.initData.pagination.pageSize &&
                that.pageParam.initData.pagination.total % that.pageParam.initData.pagination.pageSize === 1
              ) {
                that.$refs.formPage.getTableData(true)
                return
              }
              // if (
              //   that.$refs.formPage.pageParam.initData.total > that.$refs.formPage.pageParam.initData.pageSize &&
              //   that.$refs.formPage.pageParam.initData.total % that.$refs.formPage.pageParam.initData.pageSize === 1
              // ) {
              //   // that.$refs.formPage.pageParam.initData.pageNum -= 1
              //   // that.$refs.formPage.pageParam.initData.total -= 1
              //   that.$refs.formPage.getTableData(true)
              //   return
              // }
            }
            that.$refs.formPage.getTableData();//调用表单中的刷新
            that.dialog.visable = false ;
        }else{
            that.$message({
                message:res.data.message ,
                type : 'error'
            });
        }
      })
    }

            // throttleServe: throttle((methods, params, url, that,from,name="") => {
        //   //from 来自于哪个按钮 如编辑update删除delete新增add等
        //   //name 需要操作的名称 
        //   console.log('methods', methods)
        //   actionByType(methods, url, params,that,from,name="").then(res => {
        //     if(res.status == 200 && res.data.code == 200){
        //         let message="成功"
        //         console.log({from})
        //         switch (from){
        //           case "add": 
        //             message=`新增${name}成功!`;
        //             break
        //           case "update": 
        //             message=`编辑${name}成功!`;
        //             break
        //           case "delete": 
        //              message=`删除${name}成功!`;
        //              break
        //           case "deleteAll":
        //              message=`批量删除${name}成功!`;
        //              break
        //           default:
        //              message=`新增${name}成功!`;
        //         }
        //         console.log({message})
        //         that.$message({
        //             message ,
        //             type : 'success'
        //         });
        //         that.$refs.formPage.getTableData();//调用表单中的刷新
        //         that.dialog.visable = false ;
        //     }else{
        //         that.$message({
        //             message:res.data.message ,
        //             type : 'error'
        //         });
        //     }
        //   })
        // }, 1000),